/*
 * @copyright fxsen <br/>
 * @date 2022/3/31 22:24 <br/>
 * @email <a href="mailto:fxsen@foxmail.com">
 * @author fxsen
 */
package com.fxsen.commons.web.core.config;

import com.fxsen.commons.core.properties.FileProperties;
import com.fxsen.commons.web.core.interceptor.JwtAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Jwt 拦截器配置
 * @author fxsen
 * @date 2022/3/31 22:24
 * @email <a href="mailto:fxsen@foxmail.com">
 */
@Configuration
public class JwtInterceptorConfig implements WebMvcConfigurer {
    /** 文件配置 */
    @Autowired
    private FileProperties properties;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //默认拦截所有路径
        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/**")
                //排除拦截的路径
                .excludePathPatterns("/login", "/register", "/login/doLogin", "/user/register","/login/getCaptcha",
                 "/druid/**", "/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui.html/**","/doc.html","favicon.ico","/avatar/**","/file/**");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        FileProperties.ElPath path = properties.getPath();
        String avatarUtl = "file:" + path.getAvatar().replace("\\","/");
        String pathUtl = "file:" + path.getPath().replace("\\","/");
        registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
        registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    @Bean
    public JwtAuthenticationInterceptor authenticationInterceptor() {
        return new JwtAuthenticationInterceptor();
    }
}
